using System;
using System.Threading;
using System.Windows.Forms;
using Client.UIHelper;
using ClientForms;

namespace SampleApp
{
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main()
        {
            Application.EnableVisualStyles();

            // Add and event handler to handle unhandled exceptions.
            Application.ThreadException += Application_ThreadException;


            //launch the splash screen on a seperate thread
            //The first value passed in disables the timer that automatically closes the form.
            //The second value passed is the fade constant, which determines the change in form opacity for every interval of
            //the fadeTimer
            //The third value is the interval for the fadeTimer in ms
            int splashTimeout = 0;
            double fadeConstant = 0.05;
            int fadeTimer = 35;


            SplashForm.Showasyncsplash(splashTimeout, fadeConstant, fadeTimer);
            SplashForm.StatusText = "Initializing Data Store";
            var ds = new DataStore(SplashForm.FormRef);
            ds.DataStoreFailed += DataStore_ProccessingFailed;


            //wait until the web service if finished processing
            while (ds.GetProductState == ProductState.WebServiceInProgress)
            {
                Thread.Sleep(1000);
            }

            if (ds.GetProductState == ProductState.NoDataStore)
            {
                //notify the user that there was a problem in initializing the app, and verify that they
                //have internet connectivity
            }

            if (ds.GetProductState != ProductState.NoDataStore)
            {
                SplashForm.StatusText = "Data store initialized";
                Thread.Sleep(2500);
                //start the splash screen closing
                SplashForm.closeAsyncSplash();
            }
            else
            {
                SplashForm.StatusText = "Error initializing datastore";
                Thread.Sleep(2500);
            }


            Application.Run(new Form1());
        }

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            Exception ex = e.Exception;

            try
            {
                var exForm = new ExceptionForm(ex.Message, ex.Source, ex.StackTrace, "unhandled Exception");
                exForm.ShowDialog();
            }
            catch (DataStoreAppConfigException datStoreAppConfigException)
            {
                //Form cannot be used until the datstore has been initialized.
            }
        }

        private static void DataStore_ProccessingFailed(object sender, ProcessingFailedEvent e)
        {
            SplashForm.ShowDialogBox(e.FailureInformation.Message);
        }
    }
}